package com.leeon.typeinfo.factory;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Part{

	@Override
	public String toString() {
		return getClass().getSimpleName();
	}
	
	static List<Class<? extends Part>> partFactoryies =
			new ArrayList<Class<? extends Part>>();
	
	static{
		partFactoryies.add(FuelFilter.class);
		partFactoryies.add(AirFilter.class);
		partFactoryies.add(CabinFilter.class);
		partFactoryies.add(OilFilter.class);
		partFactoryies.add(FanBelt.class);
	}
	
	private static Random rand = new Random(47);
	
	public static Part createRandom(){
		int n = rand.nextInt(partFactoryies.size());
		try {
			return partFactoryies.get(n).newInstance();
		} catch (InstantiationException e) {
			throw new RuntimeException();
		} catch (IllegalAccessException e) {
			throw new RuntimeException();
		}
	}
	
}

class Filter extends Part{}
class FuelFilter extends Filter{
	
	public static class Factory implements com.leeon.typeinfo.factory.Factory<FuelFilter>{
		public FuelFilter create(){
			return new FuelFilter();
		}
	}
}

class AirFilter extends Filter{
	public static class Factory implements com.leeon.typeinfo.factory.Factory<AirFilter>{
		public AirFilter create(){
			return new AirFilter();
		}
	}
}

class CabinFilter extends Filter{
	public static class Factory implements com.leeon.typeinfo.factory.Factory<CabinFilter>{
		public CabinFilter create(){
			return new CabinFilter();
		}
	}
}

class OilFilter extends Filter{
	public static class Factory implements com.leeon.typeinfo.factory.Factory<OilFilter>{
		public OilFilter create(){
			return new OilFilter();
		}
	}
}

class Belt extends Part{}

class FanBelt extends Belt{
	public static class Factory implements com.leeon.typeinfo.factory.Factory<FanBelt>{
		public FanBelt create(){
			return new FanBelt();
		}
	}
}






public class RegisteredFactories {

	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			System.out.println(Part.createRandom());
		}
	}
	
}
